﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using Longbow.Data;

namespace HuaCheng.DataAccess
{
    /// <summary>
    /// 用户组的数据库操作
    /// </summary>
    public class UserGroupHelper
    {
        internal const string RetrieveUsergroupinfoDataKey = "UserGroupHelper-RetrieveUsergroupinfo";
        internal const string RetrieveUsergroupinfoForGroupUserDataKey = "UserGroupHelper-RetrieveUsergroupinfoForGroupUser";
        /// <summary>
        /// 验证用户组名是否存在
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static bool UserGroupIsExist(string groupname)
        {
            bool UserFlag = false;
            List<UserGroup> groupSet;
            groupSet =RetrieveUsergroupinfo();
            if (groupSet.Where(o => o.GroupName == groupname).ToList().Count > 0)
            {
                UserFlag = true;
            }
            return UserFlag;
        }
        /// <summary>
        /// 新增用户组信息
        /// </summary>
        /// <param name="usergroup"></param>
        public static void InsertUserGroup(UserGroup usergroup)
        {
            if (usergroup == null) throw new ArgumentNullException("usergroup");
            string sql = "insert into usergroup(groupname,remark,popedomcodeex) values (@groupname,@remark,@popedomcodeex)";
            try
            {
                using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
                {
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@groupname", usergroup.GroupName));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@remark", usergroup.Remark));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@popedomcodeex", usergroup.PopedomCodeex));
                    DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
                }
            }
            catch (Exception ex) {  Console.WriteLine(ex.Message + ex.StackTrace); }
        }
        /// <summary>
        /// 更新用户组信息
        /// </summary>
        /// <param name="usergroup"></param>
        public static void UpdateUserGroup(UserGroup usergroup)
        {
            if (usergroup == null) throw new ArgumentNullException("usergroup");
            string sql ="update usergroup set popedomcodeex=@popedomcodeex,remark=@remark where groupname=@groupname";
            try
            {
                using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
                {
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@groupname", usergroup.GroupName));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@remark", usergroup.Remark));
                    cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@popedomcodeex", usergroup.PopedomCodeex));
                    DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
                }
            }
            catch (Exception ex) {  Console.WriteLine(ex.Message + ex.StackTrace); }
        }
        /// <summary>
        /// 查询用户组信息(用于用户选项卡中用户组数据源绑定)
        /// </summary>
        /// <returns></returns>
        public static List<UserGroup> RetrieveUsergroupinfo() 
        {
            //这里查询不设缓存，否则将造成用户组与用户两个界面的用户组下拉框联动
            List<UserGroup> usergroupSet = new List<UserGroup>();
            string sql = "select groupname,popedomcodeex from usergroup";
            DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
            try
            {
                using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            UserGroup usergroup = new UserGroup()
                            {
                                GroupName = (string)reader["groupname"],
                                PopedomCodeex = (string)reader["popedomcodeex"]
                            };
                            usergroupSet.Add(usergroup);
                        }
                    }
                }
            }
            catch (Exception ex) {  Console.WriteLine(ex.Message + ex.StackTrace); }
            return usergroupSet;
        }
        /// <summary>
        /// 根据用于组名称查询popedomcodeex
        /// </summary>
        /// <param name="groupType"></param>
        /// <returns></returns>
        public static string RetrieveMenuCode(string groupType)
        {
            var userGroupInfo = RetrieveUsergroupinfo();
            return ((userGroupInfo.Where(o => o.GroupName == groupType).FirstOrDefault() ?? new UserGroup()).PopedomCodeex);
        }
        /// <summary>
        /// 删除用户组
        /// </summary>
        /// <param name="groupname"></param>
        public static void DeleteUserGroup(string groupname)
        {
            string key = string.Format("{0}-{1}", UserInfoHelper.RetrieveUsernameBygroupDataKey, groupname);
            string sql = "delete from usergroup where groupname=@groupname;";
            sql += "delete from userinfo where groupname=@groupname";
            using (TransactionPackage transaction = DBAccessManager.SqlDBAccess.BeginTransaction())
            {
                try
                {
                    using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
                    {
                        cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@groupname", groupname));
                        DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
                        transaction.CommitTransaction();
                    }
                }
                catch (Exception ex)
                {
                     Console.WriteLine(ex.Message + ex.StackTrace);
                    transaction.RollbackTransaction();
                }
            }
        }
    }
}
